package com.ingen.chakhleh.junit;

import java.util.ArrayList;
import java.util.Collection;

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.google.appengine.api.datastore.Text;
import com.ingen.chakhleh.core.api.domain.Cusine;
import com.ingen.chakhleh.core.api.domain.Dish;
import com.ingen.chakhleh.core.api.domain.Restaurant;
import com.ingen.chakhleh.core.api.service.admin.ApplicationAdminService;
import com.ingen.chakhleh.core.api.service.core.ApplicationCoreService;

public class RemoteBulkLoader {

	private ApplicationAdminService httpRemoteApplicationAdminServiceClient;
	private ApplicationCoreService httpRemoteApplicationCoreServiceClient;
	private ClassPathXmlApplicationContext appContext;
	
	@Before
	public void init(){
		appContext = new ClassPathXmlApplicationContext(new String[]{"spring-rmi-client-service.xml"});
		httpRemoteApplicationAdminServiceClient=(ApplicationAdminService)appContext.getBean("httpRemoteApplicationAdminServiceClient");
		httpRemoteApplicationCoreServiceClient=(ApplicationCoreService)appContext.getBean("httpRemoteApplicationCoreServiceClient");
	}
	
	@Test
	public void execute(){
		System.out.println(httpRemoteApplicationCoreServiceClient.getDishesWithNamePrefixedWith("D"));
		//restaurantAndDishUploader();
		//cusineUploader();
	}
	
	
	
	
	
	public void cusineUploader(){
		System.out.println("Uploading Cusines");
		httpRemoteApplicationAdminServiceClient.createCusines(this.getCusines());
		System.out.println("Uploaded successfully");
	}
	
	public void restaurantAndDishUploader(){
		
		
		for(Restaurant restaurant : getResaturants()){
			Collection<Restaurant> restaurants = httpRemoteApplicationCoreServiceClient.getRestaurantsWithName(restaurant.getName());
			if(restaurants==null || restaurants.isEmpty()){
				System.out.println("No Restaurant found with name:" +restaurant.getName());
				System.out.println("Creating Restaurant: " +restaurant.getName());
				restaurant = httpRemoteApplicationAdminServiceClient.ceateRestaurant(restaurant);
				System.out.println("Restaurant created successfully");
			}
			else if(restaurants.size()==1){
				System.out.println("one Restaurant found with name:" +restaurant.getName());
				restaurant = restaurants.iterator().next();
			}
			else {
				System.out.println("Multiple Restaurants were found with name:" +restaurant.getName());
				System.out.println("Loader will skip over");
			}
			
			for(Dish dish : getDishes()){
				
				System.out.println("Adding Dish: " +dish.getName()+ " to Restaurant: " +restaurant.getName());
				httpRemoteApplicationAdminServiceClient.addDishToRestaurant(restaurant, dish);
				System.out.println("Association successfully persisted.");
			}
		}
	}
	
	
	
	public Collection<Dish> getDishes(){
		Dish dish1 = new Dish();
		dish1.setName("Dosa");
		dish1.setDescription(new Text("A south indian dish."));
		
		Dish dish2 = new Dish();
		dish2.setName("Samosa");
		dish2.setDescription(new Text("A south indian dish."));
		
		Dish dish3 = new Dish();
		dish3.setName("Chicken Cury");
		dish3.setDescription(new Text("A north indian dish."));
		
		Dish dish4 = new Dish();
		dish4.setName("Chicken Tandori");
		dish4.setDescription(new Text("A north indian dish."));
		
		Dish dish5 = new Dish();
		dish5.setName("Dalma");
		dish5.setDescription(new Text("A dish from orissa."));
		
		Dish dish6 = new Dish();
		dish6.setName("Pokhala");
		dish6.setDescription(new Text("A dish from orissa."));
		
		Collection<Dish> dishCollection = new ArrayList<Dish>();
		
		dishCollection.add(dish1);
		dishCollection.add(dish2);
		dishCollection.add(dish3);
		dishCollection.add(dish4);
		dishCollection.add(dish5);
		dishCollection.add(dish6);
		
		return dishCollection;
	}
	
	public Collection<Restaurant> getResaturants(){
		Restaurant restaurant1 = new Restaurant();
		restaurant1.setName("Rasoi");
		
		
		Restaurant restaurant2 = new Restaurant();
		restaurant2.setName("Kabab Curry");
		
		Collection<Restaurant> restaurants = new ArrayList<Restaurant>();
		restaurants.add(restaurant1);
		restaurants.add(restaurant2);
		
		return restaurants;
	}
	
	public Collection<Cusine> getCusines(){
		Cusine cusine1 = new Cusine();
		cusine1.setName("Indian");
		
		Cusine cusine2 = new Cusine();
		cusine2.setName("Italian");
		
		Cusine cusine3 = new Cusine();
		cusine3.setName("Chinese");
		
		Cusine cusine4 = new Cusine();
		cusine4.setName("Mexican");
		
		Cusine cusine5 = new Cusine();
		cusine5.setName("French");
		
		Cusine cusine6 = new Cusine();
		cusine6.setName("Afrian");
		
		ArrayList<Cusine> list = new ArrayList<Cusine>();
		list.add(cusine1);
		list.add(cusine2);
		list.add(cusine3);
		list.add(cusine4);
		list.add(cusine5);
		list.add(cusine6);
		
		return list;
	}
	
}
